

/*
 * @(#)MaintWorkflowStepFormEquatAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.form.bean.FormElement;
import com.dcivision.form.bean.FormElementType;
import com.dcivision.form.bean.FormPage;
import com.dcivision.form.bean.FormRecord;
import com.dcivision.form.bean.FormSelectionField;
import com.dcivision.form.bean.MtmFormPageFormSection;
import com.dcivision.form.bean.MtmFormSectionFormElement;
import com.dcivision.form.dao.FormElementDAObject;
import com.dcivision.form.dao.FormPageDAObject;
import com.dcivision.form.dao.FormRecordDAObject;
import com.dcivision.form.dao.FormSelectionFieldDAObject;
import com.dcivision.form.dao.FormSelectionFieldOptionDAObject;
import com.dcivision.form.dao.MtmFormPageFormSectionDAObject;
import com.dcivision.form.dao.MtmFormSectionFormElementDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.workflow.bean.MtmWorkflowRecordObjectLink;
import com.dcivision.workflow.bean.WorkflowStepFormEquat;
import com.dcivision.workflow.dao.MtmWorkflowRecordObjectLinkDAObject;

/**
 * MaintWorkflowStepFormEquatAction.java
 * @author      Tony Chen
 * @company     DCIVision Limited
 * @creation date   03/09/2004
 * @version     $Revision: 1.8 $
 */


public class MaintWorkflowStepFormEquatAction extends com.dcivision.framework.web.AbstractMaintAction {

  public static final String REVISION = "$Revision: 1.8 $";

  
  /** Creates a new instance of MaintMultipleFileUploadAction */
  public MaintWorkflowStepFormEquatAction() {
    super();
  }

  /** getFunctionCode
   *
   * Abstract function which sub-class should implement to return the corresponding
   * function code.
   *
   * @return   The function code
   */
  public String getFunctionCode() {
    return SystemFunctionConstant.WORKFLOW_TASK;
  }

  /** getMajorDAOClassName
   *
   * Abstract function which sub-class should implement to return the corresponding
   * major DAO class name used in this class.
   *
   * @return   The DAO class name
   */
  public String getMajorDAOClassName() {
    return "com.dcivision.workflow.dao.WorkflowStepDAObject";
  }

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    MaintWorkflowStepFormEquatForm maintForm = (MaintWorkflowStepFormEquatForm)form;

    String opMode = (maintForm != null) ? maintForm.getOpMode() : request.getParameter("opMode"); //maintForm.getOpMode();
    String navMode = (maintForm != null) ? maintForm.getNavMode() : request.getParameter("navMode"); //maintForm.getNavMode();
    //set the default route
    if(Utility.isEmpty(maintForm.getFormEquationListValue())){
      String  formEquatListValue = MessageResourcesFactory.getMessage(this.getSessionContainer(request).getSessionLocale() , "workflow.label.default_route") + ";Y;;;;;N;dd-MM-yyyy HH:mm:ss";
      maintForm.setFormEquationListValue(formEquatListValue);
    }
    
    if (GlobalConstant.OP_MODE_REFRESH.equals(opMode)) {
      // Set the function code for current action.
      ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
      if (forward != null) {
        return forward;
      }
      refreshFormData(mapping, maintForm, request, response);
      return mapping.findForward(GlobalConstant.NAV_MODE_REFRESH);
    } else {
      return super.execute(mapping, form, request, response);
    }
  }

  public void refreshFormData(
        ActionMapping mapping, 
        AbstractActionForm form, 
        HttpServletRequest request, 
        HttpServletResponse response) 
  throws ApplicationException {
    List formElementList = new ArrayList();
    List formRecordList = new ArrayList();
    String optionElementIDs = "";
    
    try {
      SessionContainer sessionContainer = this.getSessionContainer(request);
      Connection conn = this.getConnection(request);
      MtmWorkflowRecordObjectLinkDAObject objectLinkDAO = new MtmWorkflowRecordObjectLinkDAObject(sessionContainer,conn);
      FormRecordDAObject formRecordDAO = new FormRecordDAObject(sessionContainer,conn);
      FormPageDAObject formPageDAO = new FormPageDAObject(sessionContainer,conn);
      FormElementDAObject formElementDAO = new FormElementDAObject(sessionContainer,conn);
      FormSelectionFieldDAObject fieldDAO = new FormSelectionFieldDAObject(sessionContainer,conn);
      FormSelectionFieldOptionDAObject optionDAO = new FormSelectionFieldOptionDAObject(sessionContainer,conn); 
            
      MtmFormPageFormSectionDAObject pageSectionDAO = new MtmFormPageFormSectionDAObject(sessionContainer,conn);
      MtmFormSectionFormElementDAObject sectionElementDAO = new MtmFormSectionFormElementDAObject(sessionContainer,conn);
      
      MaintWorkflowStepFormEquatForm maintForm = (MaintWorkflowStepFormEquatForm)form;
      
      String workflowRecordID = request.getParameter("workflowRecordID");
      String formEquationListValue = maintForm.getFormEquationListValue();
      String[] formEquationArr = null;
      String formEquationItem = maintForm.getFormEquationItem();

      //get the formElementList    
      Integer formRecordID = null;
      if( !Utility.isEmpty(maintForm.getFormRecordID()) ){
        formRecordID = new Integer( maintForm.getFormRecordID() );
      }
      
      if(!Utility.isEmpty(workflowRecordID) ){
        List objectLinkList = objectLinkDAO.getListByWorkflowRecordID(new Integer(workflowRecordID));
        for(int i=0; i<objectLinkList.size(); i++){
          MtmWorkflowRecordObjectLink tmpObjectLink = (MtmWorkflowRecordObjectLink)objectLinkList.get(i);
          if(GlobalConstant.OBJECT_TYPE_FORM.equals(tmpObjectLink.getObjectType())){            
            if (i == 0 && Utility.isEmpty(maintForm.getFormRecordID()) ) {
              formRecordID = tmpObjectLink.getObjectID();
            }
            FormRecord formRecord = (FormRecord)formRecordDAO.getObjectByID(tmpObjectLink.getObjectID());
            formRecordList.add(formRecord);
          }  
        }       
      
      }
      
      if(!Utility.isEmpty(formRecordID)){
        List formPageList = formPageDAO.getDetailListByFormRecordID(formRecordID);
        for(int j=0; j<formPageList.size(); j++){
          FormPage formPage = (FormPage)formPageList.get(j);
          List pageSectionList = pageSectionDAO.getListByPageID(formPage.getID());
          for (int k=0; k<pageSectionList.size(); k++){
            MtmFormPageFormSection pageSection = (MtmFormPageFormSection)pageSectionList.get(k);
            List sectionElementList = sectionElementDAO.getListBySectionID(pageSection.getSectionID());
            for(int n=0; n<sectionElementList.size(); n++){
              MtmFormSectionFormElement sectionElement = (MtmFormSectionFormElement)sectionElementList.get(n);
              FormElement formElement = (FormElement)formElementDAO.getObjectByID(sectionElement.getElementID());
              if(FormElementType.ELEMENT_TEXT.equals(formElement.getElementType()) ||
                 FormElementType.ELEMENT_TEXT_AREA.equals(formElement.getElementType()) ||
                 FormElementType.ELEMENT_DATE_PICKER.equals(formElement.getElementType()) ||
                 FormElementType.ELEMENT_CHECK_BOX.equals(formElement.getElementType()) || 
                 FormElementType.ELEMENT_SELECTION_BOX.equals(formElement.getElementType()) ||
                 FormElementType.ELEMENT_RADIO_BUTTON.equals(formElement.getElementType()) ){
                
                formElement.setFormRecordID(formRecordID.toString());
                formElement.setFormPageID(formPage.getID().toString());
                formElement.setFormSectionID(pageSection.getSectionID().toString());
                formElementList.add(formElement);
                
                if(FormElementType.ELEMENT_CHECK_BOX.equals(formElement.getElementType()) || 
                   FormElementType.ELEMENT_SELECTION_BOX.equals(formElement.getElementType()) ||
                   FormElementType.ELEMENT_RADIO_BUTTON.equals(formElement.getElementType()) ){
                    optionElementIDs +=  WorkflowStepFormEquat.ELEMENT_PREFIX_CHAR + formElement.getID() + ",";
                }
                
              }
            }
          }
        }
      }
       
      
      if(!Utility.isEmpty(formEquationListValue)){
        formEquationArr = TextUtility.splitString(formEquationListValue,"$$");
      }
      
      String[] deleteItemArr = request.getParameterValues("deleteItem");
      boolean checked = false;
      formEquationListValue = "";
      
      for(int j=0; !Utility.isEmpty(formEquationArr) &&  j<formEquationArr.length; j++){
        checked = false;
        for(int i=0; !Utility.isEmpty(deleteItemArr) && i<deleteItemArr.length; i++){
          if(j == Integer.parseInt(deleteItemArr[i])){
            checked = true;
            break;
          }    
        }
        if(!checked){          
          if(Utility.isEmpty(formEquationListValue)){
            formEquationListValue += formEquationArr[j];  
          }else{
            formEquationListValue += "$$" + formEquationArr[j];
          }
        }
      }
      
      if(Utility.isEmpty(formEquationListValue) && !Utility.isEmpty(formEquationItem) ){
        formEquationListValue = formEquationItem;
      } else if(!Utility.isEmpty(formEquationItem)) {
        formEquationListValue += "$$" + formEquationItem;  
      }
            
      //get the options
      List optionList = new ArrayList();
      if( Utility.isEmpty(maintForm.getFormElement()) ){
        FormElement formElement = (FormElement)formElementList.get(0);
        maintForm.setFormElement( WorkflowStepFormEquat.ELEMENT_PREFIX_CHAR + formElement.getID() );
      }
      
      if( !Utility.isEmpty(maintForm.getFormElement()) ){
        String elementID = maintForm.getFormElement().substring(1);
        FormElement element = (FormElement) formElementDAO.getObjectByID(new Integer(elementID));
        if(FormElementType.ELEMENT_CHECK_BOX.equals(element.getElementType()) || 
            FormElementType.ELEMENT_SELECTION_BOX.equals(element.getElementType()) ||
            FormElementType.ELEMENT_RADIO_BUTTON.equals(element.getElementType()) ){
          FormSelectionField field = (FormSelectionField) fieldDAO.getByElementID(element.getID());
          optionList = optionDAO.getListBySelectionFieldID(field.getID());        
          maintForm.setOperationType(WorkflowStepFormEquat.OPERATION_TYPE_STRING);
        }
      }      
      
      maintForm.setRouteNameListValue(maintForm.getRouteNameListValue());      
      maintForm.setFormEquationListValue(formEquationListValue);
      maintForm.setOptionElementIDs(optionElementIDs);
      maintForm.setFormRecordID(formRecordID.toString());
      
      request.setAttribute("formElementList", formElementList);
      request.setAttribute("formRecordList", formRecordList);
      //request.setAttribute("formRecordID", formRecordID);
      request.setAttribute("optionList", optionList);
    } catch (ApplicationException appEx) {
      throw appEx;
    } catch (Exception e) {
      log.error(e, e);
      throw new ApplicationException(ErrorConstant.COMMON_FATAL_ERROR, e);
    } finally {
    }
  }



  
  
  
}
